
#include <stdlib.h>
#include "llist.h"

LList::LList() {

    First = NULL;
    Last = NULL;
}

LList::~LList() {
    Destroy();
}

int LList::Process(Packet *Pack, int rev) {
    e_llist *tmp;
    int ret;
    
    tmp = (rev==YATUN_FORWARD)?First:Last;
    
    while (tmp) {
	ret = tmp->Plug->Process(Pack, rev);
	if(ret) {
	    return ret; // stop processing on error !
	}
	tmp=(rev==YATUN_FORWARD)?tmp->Next:tmp->Prev;
    }    
    return 0;
}

int LList::IsReady(int rev) {
    e_llist *tmp;
    int ret;
    
    tmp = (rev==YATUN_FORWARD)?First:Last;
    
    while (tmp) {
	ret = tmp->Plug->Ready(rev);
	if(ret == 0) {
	    return 0; // stop on error !
	}
	tmp=(rev==YATUN_FORWARD)?tmp->Next:tmp->Prev;
    }    
    return 1; // sme Ready
}


void LList::Add(c_Plug_Proc *Plugin) {
    e_llist* new_element;
    
    new_element = (struct e_llist*) malloc(sizeof(struct e_llist));
    new_element->Next = NULL;
    new_element->Prev = NULL;
    new_element->Plug = Plugin;
    
    if(Last!=NULL) {
        Last->Next = new_element;
	new_element->Prev = Last;
	Last = Last->Next;
    } else {
	First = new_element;
	Last = new_element;
    }
}


void LList::Destroy() {
    e_llist *tmp;
    
    while (First) {
	tmp=First;
	free(tmp);
	First=First->Next;
    }
    
}
